package com.xinyue.auto.reader.util;

import com.xinyue.auto.bean.Query;
import com.xinyue.auto.type.Type;

public class ReturnTypeUtil implements Type{

	/**
	 * 验证查询方法返回值是否合法
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static String validate(String type, Query query) throws Exception {
		String impt = null;
		boolean pass = false;
		
		//基本类型
		if(STRING.equals(type)) {
			pass = true;
		}else if(INTEGER.equals(type)) {
			pass = true;
		}else if(LONG_.equals(type)) {
			pass = true;
		}else if(DOUBLE_.equals(type)) {
			pass = true;
		}else if(CHARACTER.equals(type)) {
			pass = true;
		}else if(BYTE_.equals(type)) {
			pass = true;
		}else if(SHORT_.equals(type)) {
			pass = true;
		}else if(FLOAT_.equals(type)) {
			pass = true;
		}else if(BOOLEAN_.equals(type)) {
			pass = true;
		}else if(OBJ_ARR.equals(type)) {//Object[]
			pass = true;
		}else if(DATE.equals(type)) {
			impt = "java.util.Date";
			pass = true;
		}else if(BIGDECIMAL.equals(type)) {
			impt = "java.math.BigDecimal";
			pass = true;
		}else if(BLOB.equals(type)) {
			impt = "java.sql.Blob";
			pass = true;
		}
		
		if(pass) {
			query.setConvert(true);
			query.setOnly(true);
			query.setReturnType(type);
			return impt;
		}
		
		//基本集合类型
		if(BOOLEAN_S.equals(type)) {
			pass = true;
		}else if(FLOAT_S.equals(type)) {
			pass = true;
		}else if(SHORT_S.equals(type)) {
			pass = true;
		}else if(BYTE_S.equals(type)) {
			pass = true;
		}else if(CHARACTER_S.equals(type)) {
			pass = true;
		}else if(DOUBLE_S.equals(type)) {
			pass = true;
		}else if(STRING_S.equals(type)) {
			pass = true;
		}else if(INTEGER_S.equals(type)) {
			pass = true;
		}else if(LONG_S.equals(type)) {
			pass = true;
		}else if(OBJ_ARR_S.equals(type)) {//List<Object[]>
			pass = true;
		}else if(DATE_S.equals(type)) {
			impt = "java.util.Date";
			pass = true;
		}else if(BIGDECIMAL_S.equals(type)) {
			impt = "java.math.BigDecimal";
			pass = true;
		}
		
		if(pass) {
			query.setWarning(true);
			query.setReturnType(type);
			return impt;
		}
		
		//Map类型
		if(MAP.equals(type)) {
			query.setReturnType("Map<String, Object>");
			query.setMap(true);
			query.setWarning(true);
			return "Map";
		}
		
		throw new Exception("returnType属性值不合法:" + type);
	}
}